﻿using System;
public interface IShape
{
    double CalculateArea();
    bool IsShapeValid();
}
public abstract class Shape : IShape
{
    public abstract double CalculateArea();
    public abstract bool IsShapeValid();
}
public class Rectangle : Shape
{
    public double Width { get; }
    public double Height { get; }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public override double CalculateArea()
    {
        return Width * Height;
    }

    public override bool IsShapeValid()
    {
        return Width > 0 && Height > 0;
    }
}
public class Square : Rectangle
{
    public Square(double sideLength) : base(sideLength, sideLength)
    {
    }
}
public class Circle : Shape
{
    public double Radius { get; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public override double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }

    public override bool IsShapeValid()
    {
        return Radius > 0;
    }
}
class Test
{
    static void Main(string[] args)
    {        
        Random random = new Random();
        IShape[] shapes = new IShape[10];//随机创建10个对象

        for (int i = 0; i < 10; i++)
        {
            int choice = random.Next(3);
            switch (choice)
            {
                case 0:
                    shapes[i] = new Rectangle(random.NextDouble() * 10, random.NextDouble() * 10);
                    break;
                case 1:
                    shapes[i] = new Square(random.NextDouble() * 10);
                    break;
                case 2:
                    shapes[i] = new Circle(random.NextDouble() * 10);
                    break;
            }
        }

        
        double totalArea = 0;
        foreach (var shape in shapes)
        {
            if (shape.IsShapeValid())
            {
                totalArea += shape.CalculateArea();// 计算面积之和
            }
            else
            {
                Console.WriteLine("含有不合法形状");
            }
        }

        Console.WriteLine("面积总和为" + totalArea);
    }
}
